package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.common.validator.group.AddGroup;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.CfgWorkArea;
import com.xhtt.modules.cfg.service.CfgWorkAreaService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 作业区域库
 * </p>
 *
 * @author feipc
 * @since 2019-02-19
 */
@Api(value = "数据字典-作业区域库", tags = "数据字典-作业区域库")
@RestController
@RequestMapping("app/sys/cfg/workarea")
public class CfgWorkAreaController extends AbstractController {

    @Autowired
    private CfgWorkAreaService cfgWorkAreaService;

    /**
     * 获取作业区域库列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取作业区域库列表(web/app)")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = cfgWorkAreaService.queryPage(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增作业区域库
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增作业区域库(web)")
    @Login
    public R add(@RequestBody CfgWorkArea cfgWorkArea) {
        ValidatorUtils.validateEntity(cfgWorkArea, AddGroup.class);
        int count = cfgWorkAreaService.count(new LambdaQueryWrapper<CfgWorkArea>().eq(CfgWorkArea::getName, cfgWorkArea.getName()).eq(CfgWorkArea::getStatus, 1));
        if (count > 0) {
            throw new RRException("名称已存在");
        }
        cfgWorkAreaService.save(cfgWorkArea);
        return R.ok();
    }

    /**
     * 删除作业区域库
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除作业区域库(web)")
    @Login
    public R delete(@RequestBody Integer[] ids) {
        CfgWorkArea c = new CfgWorkArea();
        c.setStatus(-1);
        cfgWorkAreaService.update(c, new UpdateWrapper<CfgWorkArea>().in(CfgWorkArea.ID, Arrays.asList(ids)));
        return R.ok();
    }

    /**
     * 修改作业区域库
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改作业区域库(web)")
    @Login
    public R update(@RequestBody CfgWorkArea cfgWorkArea) {
        ValidatorUtils.validateEntity(cfgWorkArea);
        int count = cfgWorkAreaService.count(new LambdaQueryWrapper<CfgWorkArea>().eq(CfgWorkArea::getName, cfgWorkArea.getName()).eq(CfgWorkArea::getStatus, 1).ne(CfgWorkArea::getId, cfgWorkArea.getId()));
        if (count > 0) {
            throw new RRException("区域名称已存在");
        }
        cfgWorkAreaService.updateById(cfgWorkArea);
        return R.ok();
    }

    /**
     * 获取作业区域库
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取作业区域库(web/app)")
    @Login
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgWorkAreaService.getById(id));
    }

}
